package cz.spock.cms.service;

import java.util.List;

import org.springframework.stereotype.Service;

import cz.spock.cms.entity.Project;
import cz.spock.core.entity.Account;
import cz.spock.core.service.BaseService;
import cz.spock.exception.InvalidDataException;
import cz.spock.exception.InvalidInputException;

@Service
public class ProjectService extends BaseService {
    
    public Project createProject(String owner, String url) {
        Long l = null;
        try {
            l = Long.parseLong(owner);
        } catch(NumberFormatException e) {
            log.fine("owner is not number");            
            throw new InvalidInputException("Ownerid must be number.", 411);            
        }
        return this.createProject(l, url);
    }
    
    public Project createProject(Long ownerId, String url) {
        // validate data
        if(ownerId == null) {
            log.fine("owner is null");            
            throw new InvalidInputException("Ownerid is required, must be number.", 411);
        }
        if(this.dao.find(Account.class, ownerId) == null) {
            log.fine("account with id "+ownerId+" not found");
            throw new InvalidDataException("account with id "+ownerId+" not found", 421);            
        }
        if(url == null) {
            log.fine("url is null");            
            throw new InvalidInputException("Url is required.", 412);
        }
        url.replaceAll("/", "");
        if(!url.startsWith("/")) {
            url = "/"  + url;
        }
        // TODO url can not contain "/"
        if (!this.isUrlUnique(url)) {
            log.fine("url (" +url+ ") is already used");
            throw new InvalidDataException("Url ("+url+") is already used.", 422);
        }
        // create project
        Project p = new Project();
        p.setOwnerId(ownerId);
        p.setUrl(url);
        // persist account and return account with id
        Long id = dao.ofy().put(p).getId();
        p.setId(id);
        return p;
    }
    
    public Project getProjectById(Long id) {
        if(id == null) {
            return null;
        }
        return dao.ofy().get(Project.class, id);
    }
    
    public Project findProjectByUrl(String url) {
        if(url == null) {
            return null;
        }
        return dao.ofy().query(Project.class).filter("url", url).get();
    }
    
    public List<Project> findProjectByOwner(Account owner) {
        if(owner == null) {
            return null;
        }
        return this.findProjectByOwner(owner.getId());
    }    
    
    public List<Project> findProjectByOwner(Long ownerId) {
        if(ownerId == null) {
            return null;
        }
        return dao.ofy().query(Project.class).filter("ownerId", ownerId).list();
    }
    
    /**
     * searches whether URL is unique
     * 
     * @param url
     * @return
     */
    public boolean isUrlUnique(String url) {
        if(dao.ofy().query(Project.class).filter("url", url).get() == null)
            return true;
        return false;
    }

}
